package org.jeecg.modules.job;

import cn.hutool.core.collection.CollectionUtil;
import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.c.entity.DActiveAlarm;
import org.jeecg.modules.c.entity.DAlarmH;
import org.jeecg.modules.c.entity.DSignalH;
import org.jeecg.modules.c.entity.MSC;
import org.jeecg.modules.c.service.*;
import org.jeecg.modules.client.ChannelManager;
import org.jeecg.modules.enmu.AttributeLength;
import org.jeecg.modules.utils.SendMessageUtils;
import org.jeecg.modules.vo.Message;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.nio.ByteBuffer;
import java.util.Date;
import java.util.List;

import static org.jeecg.modules.enmu.MessageTypeEnum.DYN_ACCESC_MODE_ACK;
import static org.jeecg.modules.enmu.MessageTypeEnum.SEND_ALARM;

/**
 * 告警数据上传任务
 */

@Component
@Slf4j
public class AlarmUploadJob implements Job {

    private static final int head = 20;
    private static final int headAck = 60;
    private static final int headRetry = 10;


    @Autowired
    private IDSignalHService dSignalHService;
    @Autowired
    private ChannelManager channelManager;

    @Autowired
    private IMSignalService mSignalService;
    @Autowired
    private IMSCService mSCService;

    @Autowired
    private IDAlarmHService dAlarmHService;

    @Autowired
    private IDActiveAlarmService dActiveAlarmService;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
       /* //从通道管理对象中获取通道列表
        MSC msc = mSCService.list().get(0);
        Channel channel = channelManager.getChannel(mSCService.list().get(0).getScid());
        //获取现在要发送的信号数据
        List<DActiveAlarm> signalHS = mSignalService.getSendAlarmData();
        //开始组装信号发送数据
        //全部处理完毕后，发送数据
        Message message = new Message();
        message.setMessageType(SEND_ALARM.getValue());
        message.setMessageSerialNo(SendMessageUtils.messageSerialNo);
        //生成随机数，保持在int范围内，并且是正数

        byte[] dataCntBytes = SendMessageUtils.padWithSpaces("0", AttributeLength.LONG_LENGTH.getLength());
        if (CollectionUtil.isNotEmpty(signalHS)) {
            dataCntBytes = SendMessageUtils.padWithSpaces(signalHS.size() + "", AttributeLength.LONG_LENGTH.getLength());
        }
        ByteBuffer content = ByteBuffer.allocate(AttributeLength.LONG_LENGTH.getLength() + AttributeLength.TALARM_LENGTH.getLength() * signalHS.size()
                + AttributeLength.LONG_LENGTH.getLength() + AttributeLength.LONG_LENGTH.getLength() * 0
        );
        content.put(dataCntBytes);
        //根据信号
        for (DActiveAlarm dSignalH : signalHS) {
            //根据数据类型判断如何发送数据，开始拼TA
            byte[] serialnoBate = SendMessageUtils.padWithSpaces(dSignalH.getSerialno() + "", AttributeLength.ALARMSERIALNO_LENGTH.getLength());
            byte[] idByte = SendMessageUtils.padWithSpaces(dSignalH.getId(), AttributeLength.ID_LEN.getLength());
            //告警时间
            byte[] timeByte = SendMessageUtils.padWithSpaces(dSignalH.getAlarmtime() + "", AttributeLength.TIME_LEN.getLength());
            //告警等级
            byte[] levenByte = SendMessageUtils.padWithSpaces(dSignalH.getAlarmlevel() + "", AttributeLength.LONG_LENGTH.getLength());
            // todo 告警标志待确认
            //byte[] statusytes = SendMessageUtils.padWithSpaces(dSignalH.getALARM() + "", AttributeLength.ENUM_LENGTH.getLength());
            //告警触发值
            byte[] valuebytes = SendMessageUtils.padWithSpaces(dSignalH.getAlarmvalue() + "", AttributeLength.ENUM_LENGTH.getLength());
            //告警描述
            byte[] descBytes = SendMessageUtils.padWithSpaces(dSignalH.getAlarmdesc(), AttributeLength.DES_LENGTH.getLength());
            content.put(serialnoBate).put(idByte).put(timeByte).put(levenByte).put("告警标志".getBytes()).put(valuebytes).put(descBytes);

        }
        //组装无效监控点
        byte[] errorBytes = SendMessageUtils.padWithSpaces("0" + "", AttributeLength.LONG_LENGTH.getLength());
        content.put(errorBytes);
        //发送已组装数据
        channel.writeAndFlush(content);
        Date updateTime = new Date();

        //发送完成后更新，发送状态和发送时间
        signalHS.stream().forEach(item -> {
            item.setUploadStatus(1);
            item.setUploadTime(updateTime);

        });
        dActiveAlarmService.updateBatchById(signalHS);*/
    }

    //心跳监测任务


}
